package ch.cm.android.teamlearn.framents;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery;

import ch.cm.android.teamlearn.R;
import ch.cm.android.teamlearn.model.Question;
import ch.cm.android.teamlearn.orm.DataBaseHelper;
import android.R.integer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class QuestionStatistics extends Fragment {

    private static final String TAG = "QuestionStatistics";
    private TextView textView;
    
    HashMap<String, Integer> map = new HashMap<String, Integer>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_question_statistic, container, false);
        return rootView;
    }

    @Override
    public void onResume() {
        textView = (TextView) getActivity().findViewById(R.id.tv_fragment_statistic_con);
        String out = "";
        super.onResume();
        try {
            Dao<Question, Integer> dao = new DataBaseHelper(getActivity()).getDao();
            List<Question> oneofAll = dao.query(dao.queryBuilder().orderBy("tag", true).prepare());
            if (oneofAll.size() > 0) {
                String firstTag = oneofAll.get(0).tag;
                for (Question question : oneofAll) {
                    if (question.tag.equals(firstTag)) {
                        map.put(firstTag, map.get(firstTag) != null ? map.get(firstTag) + 1 : 1);
                    } else {
                        firstTag = question.tag;
                        map.put(firstTag, map.get(firstTag) != null ? map.get(firstTag) + 1 : 1);
                    }
                }
                Log.d(TAG, map.toString());
                for (String key : map.keySet()) {
                    out+=key+": "+map.get(key)+"\n";
                }
                textView.setText(out);
            }else {
                Log.d(TAG, "Keine Fragen gespeichert");
                textView.setText("Keine Fragen gespeichert");
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
